package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.CrossChannelHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 所有活动的info
 */
public class ActivityInfoMgr extends TempMgr {


    private static final Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    private static final Map<Integer, Map<Integer, List<ActivityConditionInfo>>> conditionInfoMap = new ConcurrentHashMap<>();


    public static ActivityInfo getActivityInfo(int activityId) {
        if (activityId <= 0) {
            return null;
        }
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            return activityInfo;
        }

        if (!activityInfoMap.containsKey(activityId)) {
            synchronized (activityInfoMap) {

                // 从中心库读
                if (!activityInfoMap.containsKey(activityId)) {
                    ActivityInfo info = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (info != null) {
                        activityInfoMap.put(info.getActivityId(), info);
                    }
                }

                // 从跨地区中心库读
                if (!activityInfoMap.containsKey(activityId)) {
                    long crossServerId = CrossBaseServer.getInstance().getServerId();
                    if (CrossChannelHelper.isCross3(crossServerId)) { // 只有跨地区服务器才去读
                        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
                        ActivityInfo info = ChannelActivityBussiness.getActivityInfoById(dbParam, activityId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
                        if (info != null) {
                            activityInfoMap.put(activityId, info);
                        }
                    }
                }

                // 异常日志
                if (!activityInfoMap.containsKey(activityId)) {
                    getLogger().error("can not find activityInfo!!! activityId = {}", activityId);
                }
            }
        }
        return activityInfoMap.get(activityId);
    }

    private static void loadConditionInfoFromDb(int activityId) {
        synchronized (conditionInfoMap) {
            if (!conditionInfoMap.containsKey(activityId)) {
                ActivityInfo activityInfo = getActivityInfo(activityId);
                if (activityInfo != null) {
                    // 读库
                    List<ActivityConditionInfo> list = null;
                    if (ActivityHelper.isCrossChannelActivityType(activityInfo.getType())) {
                        list = ChannelActivityBussiness.getActivityConditionInfoMap(ChannelConfig.CHANNEL_CENTER_DB_PARAM, Collections.singletonList(activityId)).get(activityId);
                    } else {
                        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> map1 = new ConcurrentHashMap<>();
                        Map<Integer, List<ActivityConditionInfo>> map2 = new ConcurrentHashMap<>();
                        ActivityBussiness.getActivityConditionInfoMap(map1, map2, Collections.singletonList(activityId));
                        list = map2.get(activityId);
                    }
                    // 加到map
                    Map<Integer, List<ActivityConditionInfo>> listMap = new ConcurrentHashMap<>();
                    if (list != null) {
                        for (ActivityConditionInfo conditionInfo : list) {
                            listMap.putIfAbsent(conditionInfo.getType(), new ArrayList<>());
                            listMap.get(conditionInfo.getType()).add(conditionInfo);
                        }
                    }
                    conditionInfoMap.put(activityId, listMap);
                }
            }
        }
    }

    public static List<ActivityConditionInfo> getConditionInfoList(int activityId, int conditionType) {
        List<ActivityConditionInfo> conditionInfoList = ActivityMgr.getActivityConditionInfoList(activityId);
        if (!conditionInfoList.isEmpty()) {
            List<ActivityConditionInfo> resultList = new ArrayList<>();
            for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                if (conditionInfo.getType() == conditionType) {
                    resultList.add(conditionInfo);
                }
            }
            return resultList;
        }
        if (!conditionInfoMap.containsKey(activityId)) {
            loadConditionInfoFromDb(activityId);
        }
        return conditionInfoMap.containsKey(activityId) ? conditionInfoMap.get(activityId).get(conditionType) : null;
    }

    public static int getChannelActivityMaxRank(int activityId, int conditionType) {
        List<ActivityConditionInfo> list = getConditionInfoList(activityId, conditionType);
        if (list != null && !list.isEmpty()) {
            return ActivityHelper.getMaxRank(list);
        }
        return CrossChannelHelper.getTempRankSize();
    }

    public static int getChannelRankPartNum(int activityId) {
        ActivityInfo info = getActivityInfo(activityId);
        if (info != null) {
            try {
                int value = Integer.valueOf(info.getParam6());
                if (value > 0) {
                    return value;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return CrossChannelHelper.getTempChannelRankPartNum();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
